package com.magicmovie.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.magicmovie.dto.MovieDTO;
import com.magicmovie.entity.Episode;
import com.magicmovie.entity.Movie;
import com.magicmovie.mapper.EpisodeMapper;
import com.magicmovie.mapper.MovieMapper;
import com.magicmovie.service.MovieService;
import com.magicmovie.utils.DateUtils;
import com.magicmovie.utils.OSSClientUtils;
import com.magicmovie.utils.QueryFilters;
import com.magicmovie.utils.data.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MovieServiceImpl implements MovieService {
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private EpisodeMapper episodeMapper;
    @Autowired
    private OSSClientUtils ossClientUtils;

    @Value("${oss.moviePrefix}")
    private String moviePrefix;
    private static Map<String, Object> fildMap = new HashMap<String, Object>() {
        {
            put("id", "m.id");
            put("movieName", "m.movie_name");
            put("createTime", "m.create_time");
            put("area", "m.area");
            put("clarity", "m.clarity");
            put("type", "m.type");
            put("price", "m.price");
            put("status", "m.status");
        }
    };
    @Override
    public List<Map<String, Object>> findByIndexCategoty(Integer indexCategoryId) {
        return movieMapper.findByIndexCategory(indexCategoryId);
    }

    @Override
    public Map<String, Object> findById(Integer id) {
        return movieMapper.findById(id);
    }

    @Override
    public Movie selectByPramaryKey(Integer id) {
        return movieMapper.selectByPrimaryKey(id);
    }

    @Override
    public Map<String, Object> findPurchasedMovie(Integer customerId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true, false);
        Page<Map<String, Object>> page = movieMapper.findPurchasedMovie(customerId);

        Map<String, Object> map = Maps.newHashMap();
        map.put("result", page.getResult());
        map.put("pageNum", page.getPageNum());
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public Map<String, Object> findByParams(@RequestBody Map<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true, false);
        Page<Map<String, Object>> page = movieMapper.findByParams(params);

        Map<String, Object> map = Maps.newHashMap();
        map.put("result", page.getResult());
        map.put("pageNum", page.getPageNum());
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        return map;
    }

    @Override
    public Map<String, Object> findByHotSale(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, true, false);
        Page<Map<String, Object>> page = movieMapper.findByHotSale();
        Map<String, Object> map = Maps.newHashMap();
        map.put("result", page.getResult());
        map.put("pageNum", page.getPageNum());
        map.put("pages", page.getPages());
        map.put("total", page.getTotal());
        return map;
    }


    @Override
    public void save(@RequestBody MovieDTO movie) {
        if(movie.getId()!=null){// update
            Movie movie1 = movie.getMovie();
            movieMapper.updateByPrimaryKeySelective(movie1);
        }else {//insert
            Movie movie1 = movie.getMovie();
            movieMapper.insert(movie1);
            Episode episode = new Episode();
            String []part=movie.getUrl().split("\\.");

            String suffix = part[part.length-1];
            String movieUrl = "movie/" + DateUtils.date2Str(DateUtils.yyyymmddhhmmss) + "." + suffix;
            ossClientUtils.moveFile(movie.getUrl(), movieUrl);
            //ossClientUtils.remove(movie.getUrl());
            episode.setMovieId(movie1.getId());
            String movieUrl2 = moviePrefix+"/" +movieUrl;
            episode.setUrl(movieUrl2);
            episodeMapper.insert(episode);
        }
    }

    @Override
    public void update(@RequestBody Movie movie) {
        movieMapper.updateByPrimaryKeySelective(movie);
    }

    @Override
    public List<Map<String, Object>> queryList(@RequestBody Map<String, Object> map) {
        return movieMapper.queryByParams(map);
    }

    @Override
    public Integer queryCount(Map<String, Object> map) {
        return movieMapper.queryCount(map);
    }
    @Override
    public PageModel getByFilter(@RequestBody QueryFilters filters) {
        PageHelper.startPage(filters.getPageNo(), filters.getPageSize());
        filters.format(fildMap);
        Page<Map<String, Object>> maps = movieMapper.getByFilter(filters);
        PageModel pageModel = new PageModel<>();
        pageModel.setTotalRecord(Math.toIntExact(maps.getTotal()));
        pageModel.setPageNo(maps.getPageNum());
        pageModel.setPageSize(maps.getPageSize());
        pageModel.setResults(maps.getResult());
        pageModel.setTotalPage(maps.getPages());
        return pageModel;
    }

    @Override
    public void delete(Integer id) {
        movieMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Movie selectByPrimaryKey(Integer id) {
        return movieMapper.selectByPrimaryKey(id);
    }

}
